package demo.generic;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

public class RedisDemoGeneric {

    public static void test(Jedis jedis){
        System.out.println("get 和 set 的使用");
        //清空数据库，避免上次残留的数据影响下次测试的结果
        jedis.flushAll();
        jedis.set("key1", "11");
        jedis.set("key2", "22");

//        String value = jedis.get("key1");
//        System.out.println("value = " + value);

        SetParams params = new SetParams();
        params.ex(10);
        params.xx();
        jedis.set("key1", "33", params);
        String value = jedis.get("key1");
        System.out.println("value = " + value);
    }

    public static void test2(Jedis jedis){
        System.out.println("exists 和 del");
        jedis.flushAll();

        jedis.set("key", "11");
        jedis.set("key2", "22");
        jedis.set("key3", "33");

        boolean result = jedis.exists("key");
        System.out.println("result = " + result);

        long result2 = jedis.del("key");
        System.out.println("result2 = " + result2);
        result = jedis.exists("key");
        System.out.println("result = " + result);

        long result3 = jedis.del("key", "key2", "key3");
        System.out.println("result3 = " + result3);
    }

    public static void test3(Jedis jedis){
        System.out.println("keys");
        jedis.flushAll();

        jedis.set("key", "11");
        jedis.set("key2", "22");
        jedis.set("key3", "33");
        jedis.set("key4", "44");

        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
    }

    public static void test4(Jedis jedis) throws InterruptedException {
        System.out.println("expire 和 ttl");
        jedis.flushAll();

        jedis.set("key", "11");
        jedis.expire("key", 10);

        Thread.sleep(3000);

        long time = jedis.ttl("key");
        System.out.println("time = " + time);

    }

    public static void test5(Jedis jedis){
        System.out.println("type");
        jedis.flushAll();

        jedis.set("key", "11");
        String type = jedis.type("key");
        System.out.println("type = " + type);

        jedis.lpush("key2", "11", "22", "33", "44");
        type = jedis.type("key2");
        System.out.println("type = " + type);

        jedis.hset("key3", "f1", "11");
        type = jedis.type("key3");
        System.out.println("type = " + type);

        jedis.sadd("key4", "11", "22", "33", "44");
        type = jedis.type("key4");
        System.out.println("type = " + type);

        jedis.zadd("key5", 10, "zhangsan");
        type = jedis.type("key5");
        System.out.println("type = " + type);
    }

    public static void main(String[] args) throws InterruptedException {
        //连接到 Redis 服务器上
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try(Jedis jedis = jedisPool.getResource()) {
            //redis 的各种命令，对应到 jedis 对象的各种方法
//            String pong = jedis.ping();
//            System.out.println(pong);

//            test(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);
        }
    }
}
